#!/usr/bin/python
# -*- coding: UTF-8 -*-
import json
from kpautos.engine.case import Case
from kptest.config.enum import DeviceType, HostType, DeviceUtility
from extension.test_logic.RequestBase import RequestBase


class REDFISH_ETHERNET_003(Case):
    """
        Name:
            REDFISH_ETHERNET_003
        Label:
            修改指定BMC网口信息支持修改 IPV4,IPV6及DNS server IP
        PreCondition:
            BMC web service normal
        TestStep:
            1) PATCH https://BMC_IP/redfish/v1/Managers/1/EthernetInterfaces/eth_id（eth_id是查询BMC网口集合资源信息查询的id），body里添加{
                        "StaticNameServers": [
                        "10.16.16.16",
                        "172.16.16.16",
                        "114.114.114.114"
                        ]
                 }
                需要get获取If-Match: ETag_value
        ExpectedResult:
            A) 执行响应码为200,设置和ibmc-网络配置 ，dns信息显示一致
        Parameters:

        Author:
            baixin liuya
        Last Modified:

        Remark:

    """

    # 测试执行前配置等准备操作
    def pre_test_case(self):
        self.logger.info("Pre Test Case Start.... ")
        self.device_dut = self.resource.get_device(device_type=DeviceType.Server, utility=DeviceUtility.DUT)
        self.bmc_host = self.device_dut.get_host(host_type=HostType.BMC)
        self.bmc_ip = self.bmc_host.local_ip
        self.bmc_user = self.bmc_host.username
        self.bmc_pwd = self.bmc_host.password

        self.logger.info("获取Token")
        self.request = RequestBase(self.bmc_ip, self.bmc_user, self.bmc_pwd)
        return_code, session_id = self.request.create_session()
        self.assertIsNotNone(return_code)
        self.session_id = session_id

    # 测试步骤
    def procedure(self):
        self.log_step("Step1: 查询BMC网口信息")
        url = "/redfish/v1/Managers/1/EthernetInterfaces"
        resp = self.request.get(url)
        if resp[0] != 200:
            self.assertIsNotNone(None, f"BMC网口信息查询失败：{json.dumps(resp[1], indent=4)}")
        member_count = resp[1]['Members@odata.count']
        self.logger.info(f"网口数量：{member_count}\n{json.dumps(resp[1]['Members'], indent=4)}")

        self.log_step("Step2: 逐一修改网口信息")
        members = resp[1]['Members']
        for m in members:
            m_url = m['@odata.id']
            header, rc, content = self.request.get_header_detail(m_url)
            self.logger.info(f"{m_url}:\n{json.dumps(content, indent=4)}")

            if_match_str = header['ETag']
            self.request.headers['If-Match'] = if_match_str
            json_text = {"StaticNameServers": ["10.16.16.16", "172.16.16.16", "114.114.114.114"]}
            resp = self.request.patch(m_url, json_text)

            rc = resp[0]
            self.assertIsNotNone(rc)
            content = resp[1]
            self.logger.info(f"修改网口后，回显信息：\n{json.dumps(content, indent=4)}")

    # 测试结束后恢复环境
    def post_test_case(self):
        resp = self.request.delete_session(self.session_id)
        self.assertIsNotNone(resp[0], "清除Token失败")
        self.logger.info("清除Token成功")
